package com.iplus.ydkf.ehome.module.home;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.iplus.ydkf.ehome.R;
import com.iplus.ydkf.ehome.common.net.Callback;
import com.iplus.ydkf.ehome.common.net.JsonParser;
import com.iplus.ydkf.ehome.common.net.NetClient;
import com.iplus.ydkf.ehome.config.ConString;
import com.iplus.ydkf.ehome.module.base.BaseActivity;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lhtde on 2016/6/10.
 */
public class RoomActivity extends BaseActivity {
    private String roomid;
    private String roomName;
    @Bind(R.id.rv_room)
    RecyclerView recyclerView;
    SingleAdapter adapter;
    @Bind(R.id.tv_room_title)
    TextView tv_roomTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    private JSONObject roomObject = new JSONObject();

    @Override
    public int bindLayout() {
        return R.layout.activity_room;
    }

    @Override
    public void initView(View view) {
        roomid = getIntent().getStringExtra("roomId");
        roomName = getIntent().getStringExtra("roomName");
        tv_roomTitle.setText(roomName);
        adapter = new SingleAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        toolbar.setNavigationIcon(R.drawable.icon_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData(View view) {
        NetClient.sendGetRequest(ConString.CLOUD_ROOM + "/" + roomid, new Callback<JSONObject>(JsonParser.getInstance()) {
            @Override
            public void onResponse(JSONObject jsonObject) {
                roomObject = jsonObject;
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(IOException e) {

            }
        });
    }

    @Override
    public String getFragmentName() {
        return null;
    }

    public class SingleAdapter extends RecyclerView.Adapter<SingleAdapter.ViewHolder> {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(RoomActivity.this, R.layout.item_device, null);
            view.setOnClickListener(new MyOnClickListener());
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            JSONObject room = roomObject.getJSONArray("data").getJSONObject(position);
            holder.tv_deviceName.setText(room.getString("cname"));
            holder.tv_deviceType.setText(room.getIntValue("type") == 0 ? "传感器" : "控制器");
            holder.tv_roomName.setText(roomName);
        }

        @Override
        public int getItemCount() {
            return roomObject.containsKey("data") ? roomObject.getJSONArray("data").size() : 0;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv_item_device)
            TextView tv_deviceName;
            @Bind(R.id.tv_item_deviceType)
            TextView tv_deviceType;
            @Bind(R.id.tv_item_roomName)
            TextView tv_roomName;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

        public class MyOnClickListener implements View.OnClickListener {

            @Override
            public void onClick(View v) {
                int itemPosition = recyclerView.getChildLayoutPosition(v);
                JSONObject room = roomObject.getJSONArray("data").getJSONObject(itemPosition);
                Intent intent = new Intent(RoomActivity.this, SensorActivity.class);
                intent.putExtra("type", room.getIntValue("type"));
                intent.putExtra("cname", room.getString("cname"));
                intent.putExtra("json", room.toJSONString());
                intent.putExtra("roomName", roomName);
                startActivity(intent);
            }
        }
    }
}
